package cqrtplm.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.hustcad.plm.basic.util.TyplmLoggerUtil;
import com.hustcad.plm.pdm.common.util.CommonUtil;
import com.hustcad.plm.pdm.ec.module.vo.ChgActivityVO;
import com.hustcad.plm.pdm.ec.module.vo.ECObjDataVO;
import com.hustcad.plm.pdm.ec.service.TyplmChangeLinkService;
import com.hustcad.plm.pdm.ec.service.TyplmChangeObjectLinkService;
import com.hustcad.plm.pdm.ec.service.TyplmChgNoticeQueryService;
import com.hustcad.plm.pdm.lifestage.service.TyplmCommonLifecycleService;
import com.hustcad.plm.pdm.mpmlink.service.TyplmMpmWorkFlowAdaptationService;
import com.hustcad.plm.pdm.workflow.model.entity.WorkFlowContainer;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowContainerService;
import com.ty.basic.common.ITyLifeCycleManaged;
import com.ty.basic.entity.CTyEntityBaseDO;
import com.ty.basic.entity.IdentifierEntity;
import com.ty.basic.entity.change.ChgNoticeDO;
import cqrtplm.service.RTChangeTheStatusOfTheChangedObjectService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.editor.language.json.converter.util.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : pieStar
 * @create 2025/7/19 14:32
 */
@Slf4j
@Service
public class RTChangeTheStatusOfTheChangedObjectServiceImpl implements RTChangeTheStatusOfTheChangedObjectService {

    @Resource
    private TyplmWorkFlowContainerService typlmWorkFlowContainerService;

    @Resource
    private TyplmChgNoticeQueryService typlmChgNoticeQueryService;

    @Resource
    private TyplmChangeLinkService typlmChangeLinkService;

    @Resource
    private TyplmChangeObjectLinkService typlmChangeObjectLinkService;

    @Resource
    private TyplmMpmWorkFlowAdaptationService tymMpmWorkFlowAdaptationService;

    @Resource
    private TyplmCommonLifecycleService typlmCommonLifecycleService;


    @Override
    public void changeTheStatus(BigInteger workFlowContainerId) {
        WorkFlowContainer workFlowContainer = typlmWorkFlowContainerService.selectOneByOid(workFlowContainerId);
        BigInteger ecnId = workFlowContainer.getBaseObjectOid();
        ChgNoticeDO chgNoticeDO = this.typlmChgNoticeQueryService.selectByPrimaryKey(ecnId);
        List<ChgActivityVO> chgActivityVOList = this.typlmChangeLinkService.queryECAByECO(chgNoticeDO);
        if (!CollUtil.isEmpty(chgActivityVOList)) {
            for (ChgActivityVO chgActivityVO : chgActivityVOList) {
                this.setEcaRelevanceChgBeforeObjectSpecifyState(chgActivityVO.getOid());
            }

        }
    }

    public void setEcaRelevanceChgBeforeObjectSpecifyState(BigInteger ecaId) {
        IdentifierEntity identifierEntity = new IdentifierEntity(ecaId, "ty.inteplm.change.CTyChangeActivity");
        TyplmLoggerUtil.debug(log, () -> "eca对象标识为==>【{}】", () -> new Object[]{JSON.toJSONString(identifierEntity)});
        List<ECObjDataVO> ecaBeforeObjectList = this.typlmChangeObjectLinkService.queryECObjList(identifierEntity, true);
        TyplmLoggerUtil.debug(log, () -> "根据eca获取变更前对象为==>【{}】", () -> new Object[]{JSON.toJSONString(ecaBeforeObjectList)});
        if (!CollUtil.isEmpty(ecaBeforeObjectList)) {
            List<CTyEntityBaseDO> updateList = new ArrayList();
            boolean isUpdateSubMpm = this.tymMpmWorkFlowAdaptationService.isUpdateAllMpm();
            for (ECObjDataVO ecObjDataVO : ecaBeforeObjectList) {
                // 获取实体对象
                CTyEntityBaseDO entity = CommonUtil.getEntity(new IdentifierEntity(ecObjDataVO.getOid(),ecObjDataVO.getOtype()));
                if (entity == null) {
                    continue;
                }
                if (entity instanceof ITyLifeCycleManaged) {
                    // 检查是否可以更新生命周期状态
                    if (isUpdateSubMpm && (ecObjDataVO.getOtype().equals("ty.inteplm.mpm.CTyMPMProcessPlan") || ecObjDataVO.getOtype().equals("ty.inteplm.mpm.CTyMPMOperation"))) {
                        List<IdentifierEntity> subMpmEntities = this.tymMpmWorkFlowAdaptationService.getAllObjWorkFlow(new IdentifierEntity(ecObjDataVO.getOid(), ecObjDataVO.getOtype()));
                        if (!CollectionUtils.isEmpty(subMpmEntities)) {
                            updateList.addAll(CommonUtil.getEntityListIgnoreNotExist(subMpmEntities));
                        }
                    }
                    // 将实体添加到待更新列表
                    updateList.add(entity);
                }
            }
            if (!CollectionUtils.isEmpty(updateList)) {
                // 批量更新生命周期状态
                this.typlmCommonLifecycleService.batchUpdateEntityLifecycle(updateList, "DISABLED",true, true);
            }
        }
    }
}
